﻿using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Security;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using UBCon.Ribbon.Models;

namespace UBCon.Ribbon
{
    /// <summary>
    /// Represents a selectable item in a UBCon.Ribbon.RibbonGallery
    /// </summary>
    public class RibbonGalleryItem : ButtonBase
    {
        //------------------------------------------------------
        //
        //  Constructors
        //
        //------------------------------------------------------
        #region Constructors

        /// <summary>
        /// Default static constructor
        /// </summary>
        static RibbonGalleryItem() 
        {
            Type ownerType = typeof(RibbonGalleryItem);

                    RibbonGalleryItem.DefaultStyleKeyProperty.OverrideMetadata(ownerType, 
                        new FrameworkPropertyMetadata(ownerType));
        }
        #endregion

        //------------------------------------------------------
        //
        //  Properties
        //
        //------------------------------------------------------
        #region Properties

        public bool IsSelected
        {
            get { return (bool)GetValue(IsSelectedProperty); }
            set { SetValue(IsSelectedProperty, value); }
        }

        // Using a DependencyProperty as the backing store for IsSelected.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty IsSelectedProperty = ListBoxItem.IsSelectedProperty.AddOwner(typeof(RibbonGalleryItem));

        internal RibbonGallery ParentGallery { get; set; }

        /// <summary>
        /// Gets logical parent of this parent.
        /// </summary>
        internal RibbonGallery LogicalParent 
        {
            get 
            {
                return ItemsOwner as RibbonGallery;
            }
        }

        /// <summary>
        /// Gets ItemsControl that holds this item.
        /// </summary>
        internal ItemsControl ItemsOwner 
        {
            get 
            {
                if (_itemsOwner == null) 
                {
                    _itemsOwner = ItemsControl.ItemsControlFromItemContainer(this);
                }
                return _itemsOwner;
            }
        }

        #endregion

        //------------------------------------------------------
        //
        //  Depenency Property Changed Callbacks
        //
        //------------------------------------------------------
        #region Depenency Property Changed Callbacks
        #endregion

        //------------------------------------------------------
        //
        //  Overrides
        //
        //------------------------------------------------------
        #region Overrides

        /// <summary>
        /// Raises UBCon.Ribbon.RibbonGalleryItem.Click routed event.
        /// </summary>
        protected override void OnClick()
        {
            if (LogicalParent != null) 
            {
                LogicalParent.SelectedIndex = LogicalParent.ItemContainerGenerator.IndexFromContainer(this);

                if (!LogicalParent.StaysOpenOnSelection && LogicalParent.ParentMenu!=null) 
                {
                    LogicalParent.ParentMenu.CloseDropDown();
                }
            }
            base.OnClick();

            if (LogicalParent != null&&LogicalParent.TemplatedParent!=null&&LogicalParent.TemplatedParent is QAToolbar) 
            {
                QAToolBarItemModel modelItem = LogicalParent.ItemContainerGenerator.ItemFromContainer(this) as QAToolBarItemModel;
                if (modelItem != null)
                {
                    QAToolbar qaBar = LogicalParent.TemplatedParent as QAToolbar;
                    qaBar.OnItemChecked(modelItem);
                }
            }
        }

        #endregion

        //------------------------------------------------------
        //
        //  Public Methods
        //
        //------------------------------------------------------
        #region public Methods
        #endregion

        //------------------------------------------------------
        //
        //  Internal Methods
        //
        //------------------------------------------------------
        #region Internal Methods
        #endregion

        //------------------------------------------------------
        //
        //  Private Methods
        //
        //------------------------------------------------------
        #region Private Methods
        #endregion

        //------------------------------------------------------
        //
        //  Event Handlers
        //
        //------------------------------------------------------
        #region Event Handlers
        #endregion

        //------------------------------------------------------
        //
        //  Fields
        //
        //------------------------------------------------------
        #region Fields
        
        private ItemsControl _itemsOwner;

        #endregion

        //------------------------------------------------------
        //
        //  Events
        //
        //------------------------------------------------------
        #region Events
        #endregion

        //------------------------------------------------------
        //
        //  Commands
        //
        //------------------------------------------------------
        #region Commands
        #endregion

        #region IRibbonControl Implementation

        /// <summary>
        /// Gets a boolean value indicating whether the contol's size is large
        /// </summary>
        public bool IsLarge
        {
            get { return false; }
        }

        #endregion

    }
    
}

